import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'
import { userId } from './user'
import { friendId, friendName } from './friend'



export const reverseList = ref([])

const chatRecordingList = ref([])

//页数
export const pageNum = ref(2)

export const useChatRecordingStore = defineStore('chatRecording', () =>{
    

    const getChatRecordingList = async() => {
        await axios({
            method : 'post',
            url : 'https://47.120.69.214:8787/chatRecord/getRecordListWithUser',
            data : {
                "userId1" : userId.value,
                "userId2" : friendId.value,
                "pageParam" : {
                    "current" : 1,
                    "size" : 100
                }
            },
            headers : {
                "Content-Type" : "application/json"
            },
        }).then(response => {
            chatRecordingList.value = response.data
            // console.log(chatRecordingList.value)
            reverseList.value = chatRecordingList.value.reverse()
            // console.log(reverseList.value)
            pageNum.value = 2
        })
        .catch(error => console.log(error))
    }

    const getGroupChatRecordingList = async() => {
        await axios({
            method : 'post',
            url : 'https://47.120.69.214:8787/chatRecord/getRecordListWithGroup',
            data : {
                "userId2" : friendId.value,
                "pageParam" : {
                    "current" : 1,
                    "size" : 100
                }
            },
            headers : {
                "Content-Type" : "application/json"
            },
        }).then(response => {
            chatRecordingList.value = response.data
            reverseList.value = chatRecordingList.value.reverse()
            pageNum.value = 2

        })
        .catch(error => console.log(error))
    }

    

    return {
        chatRecordingList,
        getChatRecordingList,
        getGroupChatRecordingList
    }
})


//刷新聊天记录列表
export function refreshChatRecordingList(type, current) {
    // console.log(current)
    if(type === 'user') {
        axios({
            method : 'post',
            url : 'https://47.120.69.214:8787/chatRecord/getRecordListWithUser',
            data : {
                "userId1" : userId.value,
                "userId2" : friendId.value,
                "pageParam" : {
                    "current" : current,
                    "size" : 10
                }
            },
            headers : {
                "Content-Type" : "application/json"
            },
        }).then(response => {
            // console.log(response.data)
            if(response.data.length > 0){
            chatRecordingList.value.push(...response.data)
            // console.log(chatRecordingList.value)
            reverseList.value = chatRecordingList.value.reverse()
            }
            // console.log(reverseList.value)
        })
        .catch(error => console.log(error))
    }
    else{
        axios({
            method : 'post',
            url : 'https://47.120.69.214:8787/chatRecord/getRecordListWithGroup',
            data : {
                "userId2" : friendId.value,
                "pageParam" : {
                    "current" : current,
                    "size" : 10
                }
            },
            headers : {
                "Content-Type" : "application/json"
            },
        }).then(response => {
            if(response.data.length > 0){
            chatRecordingList.value.push(...response.data)
            reverseList.value = chatRecordingList.value.reverse()
            }
        })
        .catch(error => console.log(error))
    }
}